package com.huoli.pasettle.component.datasource;



import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = SkyPayDataSourceConfig.BASE_PACKAGE_NAME,
    sqlSessionFactoryRef = SkyPayDataSourceConfig.SESSION_FACTORY_NAME)
public class SkyPayDataSourceConfig extends AbstractDataSourceConfig {

  private static final String MAPPER_LOCATION = "${spring.datasource.druid.skypay.mapperLocations}";

  public static final String BASE_PACKAGE_NAME = "com.huoli.pasettle.mapper.skypay";

  // 数据库连接参数配置前缀
  private static final String DATA_SOURCE_PREFIX = "spring.datasource.druid.skypay";

  private static final String DATA_SOURCE_NAME = "skypayDataSource";
  public static final String TRANSACTION_MANAGER_NAME = "skypayTransactionManager";
  public static final String SESSION_FACTORY_NAME = "skypaySqlSessionFactory";


  @Value(MAPPER_LOCATION)
  private String mapperLocations;

  @Override
  @ConfigurationProperties(prefix = DATA_SOURCE_PREFIX)
  @Bean(initMethod = "init", destroyMethod = "close", name = DATA_SOURCE_NAME)
  public DataSource dataSource() {
    return new DruidDataSource();
  }


  @Override
  public String getMapperLocation() {
    return mapperLocations;
  }


  // 数据源事务管理器
  @Bean(name = TRANSACTION_MANAGER_NAME)
  public DataSourceTransactionManager transactionManager() {
    return transactionManager(dataSource());
  }

  @Bean(name = SESSION_FACTORY_NAME)
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    return sqlSessionFactory(dataSource());
  }

  @Override
  public String getMybatisConfigLocation() {
    return "classpath:mybatis/skypay-mybatis-config.xml";
  }



}
